import json
import pypinyin
import os


#获取temp.json文件中的数据
def get_data(src):
    with open(src, 'r') as f:
        data = json.load(f)
    return data

#中文转拼音
def get_pinyin(word):
    s = ''
    for i in pypinyin.pinyin(word, style=pypinyin.NORMAL):
        s += ''.join(i)
    # 去除所有空格
    return s.replace(' ', '')

#创建一个MD文件放在mds文件夹中
def create_md(data):
    name=data['name']
    categories=data['categories']
    with open('mds/'+get_pinyin(name)+'.md', 'w') as f:
        f.write("---\n")
        f.write("title: "+name+"\n")
        f.write("date: 2022-02-28\n")
        f.write("tags:\n")
        f.write(" - "+name+"\n")
        f.write("categories:\n")
        f.write(" - "+categories+"\n")
        f.write("---\n")
        f.write("\n\n\n")
        f.write('<iframe src="http://localhost:8080/pdf/web/viewer.html?file='+data["url"]+'" width="100%" height="600px"></iframe>')

#创建一个MD文件放在mds文件夹中
def create_text_md(data):
    name=data['name'].split(".")[0]
    with open('mds/'+get_pinyin(name)+'.md', 'w') as f:
        f.write("---\n")
        f.write("title: "+name+"\n")
        f.write("date: 2022-02-28\n")
        f.write("tags:\n")
        f.write(" - "+name+"\n")
        f.write("categories:\n")
        f.write(" - 电子书\n")
        f.write("---\n")
        f.write("\n\n\n")
        f.write(data['content'])

# /Users/kongdechao/Documents/jquerypdf/中国文学名家名作赏析/古代文学-现代文学-当代文学-电子书/4电子书

#获取文件夹中的所有文件的内容
def get_file_content(path):
    try:
        with open(path, 'r', encoding="gbk") as f:
            content = f.read()
    except:
        try:
            with open(path, 'r', encoding="utf8") as f:
                content = f.read()
        except:
            try:
                with open(path, 'r', encoding="GB18030") as f:
                    content = f.read()
            except:
                content = ""

    return content

def get_files(src: object) -> object:
    files = []
    for root, dirs, fs in os.walk(src):
        for f in fs:
            files.append({'name': f, 'content': get_file_content(os.path.join(root, f))})
    return files


# 获取文件夹下所有文件夹名称
def get_dirs(src: object) -> object:
    dirs_name = []
    for root, dirs, fs in os.walk(src):
        for d in dirs:
            dirs_name.append('models/'+d)
    return dirs_name

def get_files_name(src: object) -> object:
    files = []
    for root, dirs, fs in os.walk(src):
        for f in fs:
            files.append(f)
    return files




if __name__ == '__main__':

    data = get_data('pdfsjson.json')
    print(data)
    data1 = get_data('pds.json')
    print(data1)
    ## 反转json数据
    data2 = data1[::-1]
    print(data2)

    for d in data:
        temp={"name":d["name"],"url":data2[d['index']]['url'],"categories":d["name"]}
        # 如果有children则遍历children
        if 'children' in d:
            for c in d['children']:
                temp['name']=c['name']
                temp['url'] =data2[c['index']]['url']
                print(temp)
                create_md(temp)
        # 否则
        else:
            print(temp)
            create_md(temp)



    # for i in data['data']['files']:
    #    create_md(i)

    #files=get_dirs("/Users/kongdechao/WebstormProjects/live2d_api/model/models")
    #print(files)





    # files=get_files('/Users/kongdechao/Downloads/中国文学名家名作赏析/古代文学-现代文学-当代文学-电子书/4电子书')
    # print(files)
    # for file in files:
    #     create_text_md(file)


